package com.amazon.dao;

import com.amazon.bo.Shop;
import com.amazon.bo.ShopGoods;
import com.amazon.bo.ShopGoodsBatch;
import com.amazon.dao.base.BaseDao;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ShopGoodsDao extends BaseDao<ShopGoods> {


    public List<Map<String,Object>> getNoPurchaseByShopId(int shopId,int classId) {

        String sql="\n" +
                "" +
                "select distinct p.id,p.product_class,t.product_class_id,p.updated_on \n" +
                "from product p \n" +
                "inner join \n" +
                "(\n" +
                "\tselect distinct en_id,sc.product_class_id \n" +
                "\tFrom browse_tree bt \n" +
                "\tinner join product_class  pc on pc.id=bt.product_class_id\n" +
                "\tinner join shop_class sc on sc.product_class_id =pc.id\n" +
                "\twhere sc.shop_id=:shopId and sc.product_class_id=:classId\n" +
                ") t on locate(t.en_id,p.product_class)>0 and  p.product_status=3 " +
                "\n" +
                "and not exists(select id from shop_goods where product_id=p.id and shop_id=:shopId) order by p.updated_on desc limit 40";

        List<Map<String,Object>> list=(List<Map<String,Object>>) execute(session -> {
            Query query = session.createNativeQuery(sql);

            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            query.setParameter("shopId",shopId);
            query.setParameter("classId",classId);
            return query.getResultList();
        });
        return list;
    }

    public List<ShopGoods> getGoodsListByBatchId(long id) {

            List<ShopGoods> shopGoodsList= execute(session -> session
                    .createNativeQuery("select * from shop_goods where batch_id=:batchId", ShopGoods.class)
                    .setParameter("batchId",id)
                    .getResultList());
            return  shopGoodsList.size()>0?shopGoodsList:null;
    }
}
